﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace skydream
{
        public class OperateIniFile
        {
            #region API函数声明

            [DllImport("kernel32")]//返回0表示失败，非0为成功
            private static extern long WritePrivateProfileString(string section, string key,
                string val, string filePath);

            [DllImport("kernel32")]//返回取得字符串缓冲区的长度
            private static extern long GetPrivateProfileString(string section, string key,
                string def, StringBuilder retVal, int size, string filePath);
            #endregion

           // Section参数、Key参数和IniFilePath不用再说，Value参数表明key的值，而这里的NoText对应API函数的def参数，它的值由用户指定，是当在配置文件中没有找到具体的Value时，就用NoText的值来代替。
           //NoText 可以为null或""
            /// <summary>
            /// 读取ini配置文件
            /// </summary>
            /// <param name="Section">节点</param>
            /// <param name="Key">key</param>
            /// <param name="NoText">空值代替</param>
            /// <param name="iniFilePath">文件名称</param>
            /// <returns></returns>
            #region 读Ini文件
            public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
            {
                if (File.Exists(iniFilePath))
                {
                    StringBuilder temp = new StringBuilder(1024);
                    GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                    return temp.ToString();
                }
                else
                {
                    return String.Empty;
                }
            }

            #endregion

           
            #region 写Ini文件
            /// <summary>
            ///  写Ini文件
            /// </summary>
            /// <param name="Section">写配置文件的节点名称</param>
            /// <param name="Key">写key</param>
            /// <param name="Value"></param>
            /// <param name="iniFilePath"></param>
            /// <returns></returns>
            public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
            {
                if (File.Exists(iniFilePath))
                {
                    long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                    if (OpStation == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }

            #endregion
        }

}